package com.lunwen.lunwen.controller;

import com.lunwen.lunwen.bean.BanJi;
import com.lunwen.lunwen.bean.Result;
import com.lunwen.lunwen.bean.XueYuan;
import com.lunwen.lunwen.services.XueYuanService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

/**
 * (XueYuan)表控制层
 *
 * @author makejava
 * @since 2020-07-20 13:22:54
 */
@RestController
@RequestMapping("api")
public class XueYuanController {

    /**
     * 服务对象
     */
    @Autowired
    private XueYuanService xueYuanService;

    @RequestMapping("selectonexueyuan")
    public Result selectOneXueYuan(@RequestParam("mingCheng") String mingCheng){
        System.out.println(mingCheng);
        Result result = new Result();
        result.setResult("200","查询成功！");
        result.setData(xueYuanService.queryByMingCheng(mingCheng));
        return result;
    }

    @RequestMapping("selectallxueyuan")
    public Result selectAllXueYuan(){
        Result result = new Result();
        XueYuan xueYuan = new XueYuan();
        result.setResult("200","查询成功！");
        result.setData(xueYuanService.queryAll(xueYuan));
        return result;
    }

    @RequestMapping("insertxueyuan")
    public Result insertXueYuan(@RequestBody XueYuan xueYuan){
        Result result = new Result();
        if (StringUtils.isEmpty(xueYuan.getXueyuanmingcheng())){
            result.setResult("201","学院名称不能为空！");
            return result;
        }
        if(xueYuanService.queryByMingCheng(xueYuan.getXueyuanmingcheng()) != null){
            result.setResult("201","该学院已存在！");
            return result;
        }
        String id = UUID.randomUUID().toString();
        xueYuan.setId(id);
        xueYuanService.insert(xueYuan);
        result.setResult("200","新增成功！",id);
        return result;
    }

}